package com.example.service;

import com.example.model.Student;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 数据初始化服务
 * 应用启动时自动创建测试数据
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DataInitService implements CommandLineRunner {
    
    private final StudentService studentService;
    
    @Override
    public void run(String... args) throws Exception {
        log.info("开始初始化测试数据...");
        
        // 检查是否已有数据
        if (studentService.countStudents() > 0) {
            log.info("数据库中已存在学生数据，跳过初始化");
            return;
        }
        
        // 创建测试学生数据
        List<Student> students = createTestStudents();
        
        // 保存测试数据
        for (Student student : students) {
            try {
                studentService.saveStudent(student);
                log.info("成功保存学生: {}", student.getName());
            } catch (Exception e) {
                log.error("保存学生数据失败: {}", student.getName(), e);
            }
        }
        
        log.info("测试数据初始化完成，共创建 {} 条学生记录", students.size());
    }
    
    /**
     * 创建测试学生数据
     */
    private List<Student> createTestStudents() {
        return Arrays.asList(
            createStudent("张三", 20, 175L, "2021001", "张大明", "李小红"),
            createStudent("李四", 21, 180L, "2021002", "李建国", "王美丽"),
            createStudent("王五", 19, 170L, "2021003", "王志强", "陈淑华"),
            createStudent("赵六", 22, 185L, "2021004", "赵国庆", "刘春花"),
            createStudent("孙七", 20, 172L, "2021005", "孙建华", "张桂兰"),
            createStudent("周八", 21, 178L, "2021006", "周明亮", "李秀英"),
            createStudent("吴九", 19, 165L, "2021007", "吴大伟", "王丽娟"),
            createStudent("郑十", 23, 182L, "2021008", "郑志远", "陈美玲"),
            createStudent("钱一", 20, 176L, "2021009", "钱进宝", "李凤霞"),
            createStudent("陈二", 22, 174L, "2021010", "陈建军", "张淑芬")
        );
    }
    
    /**
     * 创建单个学生对象
     */
    private Student createStudent(String name, Integer age, Long height, String number, 
                                String father, String mother) {
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        student.setHeight(height);
        student.setNumber(number);
        
        // 设置父母信息
        Student.Parent parent = new Student.Parent();
        parent.setFather(father);
        parent.setMother(mother);
        student.setParent(parent);
        
        // 设置带分析器的父母信息
        Student.Parents parents = new Student.Parents();
        parents.setFather(father);
        parents.setMother(mother);
        student.setParents(parents);
        
        // 设置查询信息
        Student.Query.Fuzzy fuzzy = new Student.Query.Fuzzy();
        fuzzy.setValue(name);
        fuzzy.setFuzziness(2);
        
        Student.Query query = new Student.Query();
        query.setFuzzy(fuzzy);
        student.setQuery(query);
        
        return student;
    }
}